package com.sub.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;

/**
 * @Description: TODO(使用zip4j进行解压)
 * @author wangkai
 * @date 2018年6月12日
 *
 */
public class UnZip {
	private static final Logger log = LoggerFactory.getLogger(UnZip.class);

	/**
	 * @param zipPath(zip文件路径)
	 * @param unZipPath(解压路径)
	 * @param password(zip解压密码)
	 */
	@SuppressWarnings("unchecked")
	public static void unzip(String zipPath, String unZipPath, String password) {
		if (!zipPath.toLowerCase().endsWith(".zip")) {
			log.error("非zip文件！");
			return;
		}
		try {
			File zipFile = new File(zipPath);
			ZipFile zFile = new ZipFile(zipFile); // 首先创建ZipFile指向磁盘上的.zip文件
			zFile.setFileNameCharset("GBK");
			File destDir = new File(unZipPath); // 解压目录
			if (zFile.isEncrypted()) {
				zFile.setPassword(password.toCharArray()); // 设置密码
			}
			zFile.extractAll(unZipPath); // 将文件抽出到解压目录(解压)
			List<FileHeader> headerList = zFile.getFileHeaders();
			List<File> extractedFileList = new ArrayList<File>();
			for (FileHeader fileHeader : headerList) {
				if (!fileHeader.isDirectory()) {
					File file = new File(destDir, fileHeader.getFileName());
					extractedFileList.add(file);
					System.out.println(file.getAbsolutePath() + "....");
				}
			}
		} catch (ZipException e) {
			log.error(e.getMessage());
		}
	}
public static void main(String[] args) {
	unzip("D:\\hheh\\subDT.zip", "D:\\hheh", null);
}
}
